import 'package:flutter/material.dart';
import 'package:hospitable_rental/routes/routes.dart';
import 'package:hospitable_rental/viewmodels/auth_viewmodel.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  late TextEditingController _username;
  late TextEditingController _password;

  bool _showPsd = false;

  @override
  void initState() {
    super.initState();
    _username = TextEditingController();
    _password = TextEditingController();
  }

  @override
  void dispose() {
    super.dispose();
    _username.dispose();
    _password.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("登录"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: SingleChildScrollView(
          child: Column(
            children: [
              _getUsernameWidget(),
              _getPasswordWidget(),
              SizedBox(height: 50.0),
              _getLoginButtonWidget(),
              _getRegisterWidget(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _getUsernameWidget() {
    return TextField(
      controller: _username,
      autofocus: true,
      decoration: InputDecoration(
        labelText: "用户名",
        hintText: "请输入用户名",
        icon: Icon(Icons.person),
      ),
      textInputAction: TextInputAction.next,
    );
  }

  Widget _getPasswordWidget() {
    return Stack(
      children: [
        TextField(
          controller: _password,
          decoration: InputDecoration(
            labelText: "密码",
            hintText: "请输入密码",
            icon: Icon(Icons.lock),
            suffixIcon: IconButton(
              icon: Icon(_showPsd ? Icons.visibility_off : Icons.visibility),
              onPressed: _showPassword,
            ),
          ),
          textInputAction: TextInputAction.done,
          obscureText: !_showPsd,
        ),
      ],
    );
  }

  Widget _getLoginButtonWidget() {
    return Container(
      width: double.infinity,
      height: 50.0,
      child: ElevatedButton(
        child: Text("登 录"),
        onPressed: _login,
      ),
    );
  }

  Widget _getRegisterWidget() {
    return Container(
      width: double.infinity,
      height: 50.0,
      alignment: Alignment.center,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text("还没有账号，"),
          TextButton(
            child: Text("去注册~"),
            onPressed: _register,
          )
        ],
      ),
    );
  }

  _login() async {
    var username = _username.text;
    var password = _password.text;

    context.read<AuthViewModel>().login(context, username, password);
  }

  void _register() {
    Navigator.pushReplacementNamed(context, Routes.register);
  }

  void _showPassword() {
    setState(() {
      _showPsd = !_showPsd;
    });
  }
}
